<?php
namespace app\common\model\newmodel;

use think\Model;

class AccessoryStandard extends Model
{
    protected $table = 'accessory_standard';
    protected $pk = 'id';
    /**
     * 配件规格添加
     */
    public function accessoryStandardValue()
    {
        return $this->hasMany('app\\common\\model\\newmodel\\AccessoryStandardValue', 'standard_id', 'id');
    }

    public static function standardList($accessory_id)
    {
        if(empty($accessory_id)){
            return ['code'=>0,'msg'=>'参数不完整'];
        }
        $query=self::with(['accessoryStandardValue'])->where('accessory_id',$accessory_id)->select()->toArray();
        return ['code'=>1,'msg'=>$query];
    }

    public static function standardAdd($data)
    {
        if(empty($data['accessory_id'] || empty($data['standard_name']))){
            return ['code'=>0,'msg'=>'参数不完整'];
        }
        try {
            $accessoryStandard = new self();
            $accessoryStandard->accessory_id = $data['accessory_id'];
            $accessoryStandard->standard_name =$data['standard_name'];
            $accessoryStandard->save();
            return ['code' => 1, 'msg' => '配件规格添加成功'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '配件规格添加失败：' . $e->getMessage()];
        }
    }
    /**
     * 配件规格编辑
     */
    public static function standardEdit($data)
    {
        if (empty($data['id']) || empty($data['standard_name'])) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }

        $accessoryStandard = self::get($data['id']);
        if (!$accessoryStandard) {
            return ['code' => 0, 'msg' => '配件规格不存在'];
        }

        try {
            $accessoryStandard->standard_name = $data['standard_name'];

            $accessoryStandard->save();
            
            return ['code' => 1, 'msg' => '配件规格编辑成功'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '配件规格编辑失败：' . $e->getMessage()];
        }
    }
    /**
     * 配件规格删除
     */
    public static function standardDel($id='')
    {
        if (empty($id)) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }

        $accessoryStandard = self::get($id);
        if (!$accessoryStandard) {
            return ['code' => 0, 'msg' => '配件规格不存在'];
        }

        try {
            $accessoryStandard->delete();
            return ['code' => 1, 'msg' => '配件规格删除成功'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '配件规格删除失败：' . $e->getMessage()];
        }
    }    
}